package com.xnyzc.lhy.order.service.impl.user;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.order.entity.user.OaCUser;
import com.xnyzc.lhy.order.entity.user.OaCUserMode;
import com.xnyzc.lhy.order.entity.user.wallet.OaCWallet;
import com.xnyzc.lhy.order.mapper.user.OaCUserMapper;
import com.xnyzc.lhy.order.mapper.user.OaCUserModeMapper;
import com.xnyzc.lhy.order.mapper.user.wallet.OaCWalletMapper;
import com.xnyzc.lhy.order.service.user.IOaCUserModeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户-车型关联表 服务实现类
 * </p>
 *
 * @author zhuyubin
 * @since 2019-08-09
 */
@Service
public class OaCUserModeServiceImpl extends ServiceImpl<OaCUserModeMapper, OaCUserMode> implements IOaCUserModeService {


    private static final Logger logger = LoggerFactory.getLogger(OaCUserModeServiceImpl.class);

    @Autowired
    private OaCUserMapper oaCUserMapper;
    @Autowired
    private OaCWalletMapper oaCWalletMapper;
    @Autowired
    private OaCUserModeMapper oaCUserModeMapper;

    /**
     * 部门公务
     * @param userId
     * @param token
     * @return
     */
    @Override
    public OaCUserMode getDepartmentOfficialContent(long userId, String token){
        OaCUser oaCUser = oaCUserMapper.selectById(userId);
        // 验证账号是否存在
        if (oaCUser == null){
            throw PangException.create(EErrorCode.authIdentityErr);
        }
        // 验证token
        if (!token.equals(oaCUser.getUserToken())){
            throw PangException.create(EErrorCode.tokenExpire);
        }

        OaCWallet oaCWallet = oaCWalletMapper.selectOne(
                Wrappers.<OaCWallet>lambdaQuery()
                        .eq(OaCWallet::getDeleted, "0")
                        .eq(OaCWallet::getUserId, userId)
        );
        // 验证数据
        if (oaCWallet == null){
            logger.info("未获取到用户钱包数据");
            throw PangException.create(EErrorCode.unData);
        }

        OaCUserMode oaCUserMode = oaCUserModeMapper.selectOne(
                Wrappers.<OaCUserMode>lambdaQuery()
                        .eq(OaCUserMode::getDeleted, "0")
                        .eq(OaCUserMode::getOfficeId, oaCWallet.getOfficeId())
        );

        return oaCUserMode;
    }
}
